package com.fengxing.ams.pub.ip;

import java.util.Map;

import org.assertj.core.util.Maps;

public class IPFactory {
	private static final IPServiceType defaultIpServiceType = IPServiceType.TAOBAO;
    private static Map<IPServiceType, Object> ipServiceMap = Maps.newHashMap();

    /**
     * The singleton instance of IPFactory.
     */
    private static IPFactory instance;

    /**
     * @return The singleton instance of IPFactory.
     */
    public static IPFactory getInstance() {
        if (instance == null) {
            synchronized (IPFactory.class) {
                if (instance == null) {
                	IPFactory factory = new IPFactory();
                    factory.init();
                    instance = factory;
                }
            }
        }
        return instance;
    }

    /**
     * put service into map
     */
    private void init() {
        ipServiceMap.put(IPServiceType.TAOBAO, new TaobaoIPService());
        ipServiceMap.put(IPServiceType.IP138, new IP138Service());
    }

    /**
     * return service
     * 
     * @param ipServiceType
     * @return
     */
    public IPService getService(IPServiceType ipServiceType) {
        return (IPService) ipServiceMap.get(ipServiceType);
    }
    
    /**
     * get address
     * 
     * @param ip
     * @return
     */
    public String getAddress(String ip) {
    	IPService ipService = getService(defaultIpServiceType);
    	return ipService.getAddress(ip);
    }
}
